/*-------------------<-- Start of Description-->---------------------\ | Label the variable names the same as it was in another dataset; | |---------------------<-- End of Description-->----------------------| |--------------------------------------------------------------------| |------------<-- Start of Files or Arguments Needed-->---------------| | arguments: | | fromData - the data set you want to copy label from; | | toData - the dataset you want to copy label to; | |-------------<-- End of Files or Arguments Needed-->----------------| |--------------------------------------------------------------------| |------------------<-- Start of Files Created-->---------------------| | Example: | | data a; | | attrib a label='Aaaaa!' length=$8 | | b label='Beeee!' length= 8 | | c label='Ceeee!' length=$4 | | e label='Eeeyow' length=$1 | | f length=$1; | | | | data b; | | attrib a length= 8 | | b length= 8 | | c length= 8 | | d length= 8 label='Original' | | f length= 4 label='Will be blank'; | | run; | | | | %samelabl (fromData=A, toData=B); | | | | proc contents data=b; run; | | Usage: %samelabl (fromData=, toData=); | \-------------------<-- End of Files Created-->---------------------*/ %macro samelabl (fromData=, toData=); %* Copy labels of source to destination %* Richard A. DeVenezia, 12/22/99 %*; %local this srcId dstId i nvars varname varnum varlabel labels lib mem rc; %let this = samelabl; %* open tables in utility mode; %let srcId = %sysfunc (open (&fromData,V)); %let dstId = %sysfunc (open (&toData,V)); %* create name='label' part of label statement for use in Proc DATASETS; %* only vars in both datasets will be in the statement; %if &srcId and &dstId %then %do; %let nvars = %sysfunc (attrn (&srcId, nvars)); %do i = 1 %to &nvars; %let varname = %sysfunc (varname (&srcId, &i)); %let varnum = %sysfunc (varnum (&dstId, &varname)); %if &varnum %then %do; %let varlabel = %sysfunc (varlabel (&srcId, &i)); %if %length (%superq (varlabel)) %then %let labels = &labels &varname = %str(%'%quote(&varlabel)%'); %else %let labels = &labels &varname = ' '; %end; %end; %let lib = %sysfunc (attrc (&dstId, lib)); %let mem = %sysfunc (attrc (&dstId, mem)); %let srcId = %sysfunc (close (&srcId)); %let dstId = %sysfunc (close (&dstId)); proc datasets nolist lib=&lib; modify &mem; label &labels; quit; %end; %else %do; %if &srcId = 0 %then %put ERROR: &this: Source [&fromData] could not be opened.; %else %let srcId = %sysfunc (close (&srcId)); %if &dstId = 0 %then %put ERROR: &this: Destination [&toDataination] could not be opened.; %else %let dstId = %sysfunc (close (&dstId)); %end; %mend samelabl;